import { _decorator, Component, Vec3, Graphics, Vec2, tween } from 'cc';
const { ccclass, property } = _decorator;

import PolygonNav2D from "../../PolygonNav/Script/PolygonNav2D";
import PolygonNavAgent from "../../PolygonNav/Script/PolygonNavAgent";

@ccclass('SelfPlayer')
export default class SelfPlayer extends Component {
    @property(PolygonNavAgent)
    PolygonNavAgent: PolygonNavAgent = null;
    public _isMove: boolean = false;
    private _curDir: Vec2 = null;
    private _isRobot: boolean = false;
    private _graphics: Graphics = null;
    private _polygonNav2D: PolygonNav2D;
    public get Graphics(): Graphics {
        return this._graphics;
    }

    public Init(polygonNav2D: PolygonNav2D, graphics: Graphics, isRobot: boolean = false) {
        this._polygonNav2D = polygonNav2D;
        this._graphics = graphics;
        this._isRobot = isRobot;
        let vec3 = this.node.getPosition();
        this.PolygonNavAgent.Init(polygonNav2D, new Vec2(vec3.x, vec3.y));
    }

    public SetDirPos(dir: Vec3) {
        if (dir) {
            this._curDir = new Vec2(dir.x, dir.y);
        }
        else {
            this._curDir = null;
        }
    }

    public SetTargetPos(pos: Vec3) {
        this.PolygonNavAgent.SetTargetPos(new Vec2(pos.x, pos.y));
        // this.DrawPath();
    }

    private DrawPath() {
        if (this.PolygonNavAgent.CurrentPath.length > 0) {
            this._graphics.clear();
            let pos = this.node.position;
            this._graphics.moveTo(pos.x, pos.y);
            for (let index = 0; index < this.PolygonNavAgent.CurrentPath.length; index++) {
                const element = this.PolygonNavAgent.CurrentPath[index];
                this._graphics.lineTo(element.x, element.y);
                this._graphics.stroke();
            }
        }
    }

    //添加障碍物刷新
    public AdjustPos() {
        let curPos = new Vec2(this.node.position.x, this.node.position.y);
        if (!this._polygonNav2D.PointIsValid(curPos)) {
            this.PolygonNavAgent.AdjustPos();
            let vec2 = this._polygonNav2D.GetCloserEdgePoint(curPos);
            let pos: Vec3 = new Vec3(vec2.x, vec2.y, 0);
            tween(this.node).to(0.2, { position: pos }, { easing: "sineIn" }).call(this.AdjustPosEnd.bind(this)).start();
        }
        else {
            if (this.PolygonNavAgent.HasPath) {
                let targetPos = this.PolygonNavAgent.CurrentPath[this.PolygonNavAgent.CurrentPath.length - 1];
                this.PolygonNavAgent.Stop();
                this.SetTargetPos(new Vec3(targetPos.x, targetPos.y));
            }
        }
    }

    private AdjustPosEnd() {
        this.PolygonNavAgent.SetPos(new Vec2(this.node.position.x, this.node.position.y));
        this.PolygonNavAgent.AdjustPosEnd();
        if (this.PolygonNavAgent.HasPath) {
            let targetPos = this.PolygonNavAgent.CurrentPath[this.PolygonNavAgent.CurrentPath.length - 1];
            this.PolygonNavAgent.Stop();
            this.SetTargetPos(new Vec3(targetPos.x, targetPos.y));
        }
    }

    protected update(dt: number): void {
        if (this._isRobot) {
            if (!this._isMove) {
                this._isMove = true;
                // let targetPath = new Vec3(Math.random() * 2048 - 1024, Math.random() * 2048 - 1024);
                // this.SetTargetPos(targetPath);
            }
            else {
                if (!this.PolygonNavAgent.IsMove) {
                    this._isMove = false;
                }
            }
        }

        if (!this.PolygonNavAgent.IsMove && this._graphics) {
            this._graphics.clear();
        }

        if (this._curDir) {
            if (this._isRobot) {
                this.PolygonNavAgent.SetDirPos(this._curDir, dt *0.002);
            }
            else{
                this.PolygonNavAgent.SetDirPos(this._curDir, dt * 1.2);
            }
        }
    }
}
